/*
Copyright (c) 2014, Rick Wilson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of 'Taareekah' nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import world.GenericTerrainMap;
import worldgeneration.WorldGenerator;
import util.Point;
import worldgeneration.Util;
import util.MeshInterpolator;

import parameters.*;

class InitialValue extends MeshInterpolator.InitialValueGenerator {
    GenericTerrainMap elev;
float slope = -1400.0f;
 Random rand;
 def isValidPoint(int x, int y, int size) {
    if (x < 0 || y < 0) {
        return false;
    } else if (x >= size || y >= size) {
        return false;
    }
    return true;
}
def getPointsOnPerimeter(int center_x, int center_y, int dist, int size) {
    int bl_x = center_x - dist;
    int bl_y = center_y - dist;
    int tr_x = center_x + dist;
    int tr_y = center_y + dist;
    Set<Point> result = new TreeSet<Point>();
    //bottom and top faces
    for (int x = bl_x; x <= tr_x; x++){
        if (isValidPoint(x, bl_y, size)) {
            result.add(new Point(x, bl_y));
        }
        if (isValidPoint(x, tr_y, size)) {
            result.add(new Point(x, tr_y));
        }
    }
    
    for (int y = bl_y + 1; y < tr_y; y++){
        if (isValidPoint(bl_x, y, size)) {
            result.add(new Point(bl_x, y));
        }
        if (isValidPoint(tr_x, y, size)) {
            result.add(new Point(tr_x, y));
        }
    }
    
    
    return result;
}
def getDistanceToOcean(int x, int y) {
    int cur_dist = 0;
    
    while(true) {
        cur_dist += 1;
        Set<Point> neighbors = getPointsOnPerimeter(x, y, cur_dist, elev.getSize());
        if(neighbors.size() == 0) {
            return -1;
        }
        
        for (Point p : neighbors) {
            if (elev.getValueAtPoint(p) == 0.0f) {
                return cur_dist;
            }
        }
    }
}
    public InitialValue (GenericTerrainMap elev, int size, Random rand) {
        this.elev = elev;
        this.rand = rand;
        slope = slope.div((size - 3).toFloat());
    }
    
    float getValueAtPoint(int x, int y, int mesh_size, int size) {
        if (x >= size || y >= size) {
            return 255f
        }
        if (elev.getValueAtPoint(x, y) == 0.0f) {
            return 255f;
        }
        int dist_to_ocean = getDistanceToOcean(x, y);
        if (dist_to_ocean == -1) {
            return 0.0f;
        }
        float base = slope * (float)(dist_to_ocean - 1) + 700.0f;
        if (base < 0.0f) {
            base = 0.0f;
        }
        
        base += rand.nextFloat() * 1000.0f;
        base = base.div(10.0f);
        if (base > 255.0f) {
            base = 255.0f
        }
	return base;
    }
}
int size = worldgenerator.getWorldSize();
Random rand = new Random(worldgenerator.getSeed());
GenericTerrainMap elevation = worldgenerator.getMap("elevation");
int mesh = globalparameters.getChild("terrain.rainfall.mesh").getValueAsInt();

InitialValue init = new InitialValue(elevation, size, rand);

GenericTerrainMap map = MeshInterpolator.perform(init, mesh, size);

int radius = scriptparameters.getChild("smoothradius").getValueAsInt();
float np = scriptparameters.getChild("smoothnp").getValueAsFloat();
Util.smooth(map, np, radius);
map.setIsLoaded(true);
worldgenerator.setMap("rainfall", map);